// DataViews.cpp : Implementation of CDataViews

#include "stdafx.h"
#include "DataViews.h"


// CDataViews

STDMETHODIMP CDataViews::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IDataViews
	};

	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

STDMETHODIMP CDataViews::UpdateState()
{
	//CUpdateView *_uv_view = NULL;
	//POSITION pos = NULL;

	//pos = this->m_Views.GetHeadPosition();
	//while (NULL != pos)
	//{
	//	_uv_view = m_Views.GetNext(pos);
	//	ATLASSERT(NULL != _uv_view);
	//	_uv_view->UIUpdateState(this);
	//}

	_DataViewsCol::_ColType::const_iterator _iter = this->m_coll.begin();
	
	for (; _iter != this->m_coll.end(); _iter++)
	{
		(*_iter)->GetDataView()->DataView_UpdateState((ULONG_PTR*)m_pDataObject, NULL);
	}

	return S_OK;
}

STDMETHODIMP CDataViews::UpdateEnable()
{
	//CUpdateView *_uv_view = NULL;
	//POSITION pos = NULL;

	//pos = this->m_Views.GetHeadPosition();
	//while (NULL != pos)
	//{
	//	_uv_view = m_Views.GetNext(pos);
	//	ATLASSERT(NULL != _uv_view);
	//	_uv_view->UIUpdateEnable(this, bEnable);
	//}
	_DataViewsCol::_ColType::const_iterator _iter = this->m_coll.begin();

	for (; _iter != this->m_coll.end(); _iter++)
	{
		//(*_iter)->GetDataView()->DataView_UpdateEnable((ULONG_PTR*)m_pDataObject);
	}

	return S_OK;
}

STDMETHODIMP CDataViews::UpdateData()
{
	//CUpdateView *_uv_view = NULL;
	//POSITION pos = NULL;

	//pos = this->m_Views.GetHeadPosition();
	//while (NULL != pos)
	//{
	//	_uv_view = m_Views.GetNext(pos);
	//	ATLASSERT(NULL != _uv_view);
	//	_uv_view->UIUpdateData(this);
	//}
	_DataViewsCol::_ColType::const_iterator _iter = this->m_coll.begin();

	for (; _iter != this->m_coll.end(); _iter++)
	{
		(*_iter)->GetDataView()->DataView_UpdateData((ULONG_PTR*)m_pDataObject, NULL);
	}

	return S_OK;
}

STDMETHODIMP CDataViews::WriteData()
{
	//CUpdateView *_uv_view = NULL;
	//POSITION pos = NULL;

	//pos = this->m_Views.GetHeadPosition();
	//while (NULL != pos)
	//{
	//	_uv_view = m_Views.GetNext(pos);
	//	ATLASSERT(NULL != _uv_view);
	//	if (!_uv_view->UIWriteData(this))
	//	{
	//		return FALSE;
	//	}
	//}
	_DataViewsCol::_ColType::const_iterator _iter = this->m_coll.begin();

	for (; _iter != this->m_coll.end(); _iter++)
	{
		(*_iter)->GetDataView()->DataView_WriteData((ULONG_PTR*)m_pDataObject, NULL);
	}


	return S_OK;
}


STDMETHODIMP CDataViews::ReadData()
{
	_DataViewsCol::_ColType::const_iterator _iter = this->m_coll.begin();

	for (; _iter != this->m_coll.end(); _iter++)
	{
		(*_iter)->GetDataView()->DataView_ReadData((ULONG_PTR*)m_pDataObject, NULL);
	}

	return S_OK;
}


STDMETHODIMP CDataViews::get_DataObject(ULONG_PTR** ppDataObject)
{
	*ppDataObject = this->m_pDataObject;

	return S_OK;
}


STDMETHODIMP CDataViews::put_DataObject(ULONG_PTR* pDataObject)
{
	this->m_pDataObject = pDataObject;

	return S_OK;
}


STDMETHODIMP CDataViews::Add(IDataView* pDataView)
{
	if (NULL == pDataView)
		return S_FALSE;

	CDataViewEntrust *pDataViewEntrust = new CDataViewEntrust();
	pDataViewEntrust->SetDataView(pDataView);
	this->m_coll.push_back(pDataViewEntrust);

	return S_OK;
}


STDMETHODIMP CDataViews::Move(IDataView* pDataView)
{
	if (NULL == pDataView)
		return S_FALSE;

	_DataViewsCol::_ColType::const_iterator _iter = this->m_coll.begin();

	for (; _iter!=this->m_coll.end(); _iter++)
	{
		if ( (*_iter)->GetDataView() == pDataView)
		{
			this->m_coll.remove((*_iter));
			return S_OK;
		}
	}

	return S_FALSE;
}

//
//void UIUpdateState()
//{
//
//}
//BOOL UIWriteData()
//{
//
//	return TRUE;
//}
//void UIUpdateData()
//{
//
//}
//void UIUpdateEnable(BOOL bEnable)
//{
//
//}
//void UIAddView(CUpdateView *pView)
//{
//	ATLASSERT(NULL != pView);
//	m_Views.AddTail(pView);
//}
//bool UIRemoveView(CUpdateView *pView=NULL)
//{
//	POSITION pos = NULL;
//
//	if (NULL != pView)
//	{
//		pos = m_Views.Find(pView, NULL);
//		if (NULL != pos)
//		{
//			m_Views.RemoveAt(pos);
//			return true;
//		}
//	}else
//	{
//		m_Views.RemoveAll();
//		return true;
//	}
//	return false;
//}
